package com.atguigu.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.AttrAttrGroupRelationVo;
import com.atguigu.gulimall.product.vo.CategoryAttrGroupsAttrsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;


/**
 * 属性分组
 *
 * @author lxx
 * @email 1914264274@qq.com
 * @date 2022-03-08 11:28:16
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    CategoryService categoryService;

    @Autowired
    AttrAttrgroupRelationService attrAttrgroupRelationService;

//    /product/attrgroup/{attrgroupId}/attr/relation

    /**
     * 查找当前属性分组的关联关系
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R getRelation(@PathVariable("attrgroupId") Long attrgroupId) {

        List<AttrEntity> data = attrGroupService.queryRelation(attrgroupId);
        return R.ok().put("data", data);
    }

    /**
     * 按照分类Id去发送分页请求
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Integer catelogId) {
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", page);


    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        //[2 22 234]
        //查找当前分类ID的完整路径
        Long catelogId = attrGroup.getCatelogId();
        Long[] catelogPath = categoryService.getCatelogFullPath(catelogId);
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }


    /**
     * /product/attrgroup/attr/relation/delete
     * 删除属性与分组的关联关系
     */

    @RequestMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody List<AttrAttrgroupRelationEntity> attrgroupRelationEntities) {
        attrGroupService.delBatch(attrgroupRelationEntities);
        return R.ok();
    }

    /**
     * /product/attrgroup/{attrgroupId}/noattr/relation
     */

    @GetMapping("/{attrgroupId}/noattr/relation")
    public R getReNoRRelation(@RequestParam Map<String, Object> params,
                              @PathVariable("attrgroupId") Long attrgroupId) {
        //查询当前分组可以绑定的属性(未被其他分组所引用)
        //查询当前分组所绑定的分类catId
        //查询catId都拥有哪些分组ID
        //查询分组ID都绑定了哪些属性
        //在当前分类中剔除掉这些属性
        PageUtils pageUtils = attrGroupService.getNoRelation(params, attrgroupId);

        return R.ok().put("page", pageUtils);


    }

    /**
     * 保存属性和属性分组的关联关系
     * /product/attrgroup/attr/relation
     *
     * @param list
     * @return
     */
    @PostMapping("/attr/relation")
    public R saveRelation(@RequestBody List<AttrAttrGroupRelationVo> list) {
        List<AttrAttrgroupRelationEntity> relationEntityList = list.stream().map((item) -> {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            //属性拷贝
            BeanUtils.copyProperties(item, relationEntity);
            return relationEntity;
        }).collect(Collectors.toList());
        //批量保存
        attrAttrgroupRelationService.saveBatch(relationEntityList);
        return R.ok();
    }

    /**获取分类下所有分组&关联属性
     * 根据三级分类查询出该分类的所有分组以及分组下的所有属性
     * /product/attrgroup/{catelogId}/withattr
     *
     * @return
     */
    @RequestMapping("/{catelogId}/withattr")
    public R getCatrgoryGroups(@PathVariable(value = "catelogId") Long cateLogId) {

        List<CategoryAttrGroupsAttrsVo> categoryAttrGroupsAttrsVos = attrGroupService.selectGroupsAttrsByCatLogId(cateLogId);

        return R.ok().put("data", categoryAttrGroupsAttrsVos);

    }


}
